package com.xiaoluo.lottery.web;

import com.xiaoluo.lottery.common.enums.PayBuEnums;
import com.xiaoluo.lottery.common.weixin.base.DefaultPaidCallBack;
import com.xiaoluo.lottery.common.weixin.hb.beans.RedPackRes;
import com.xiaoluo.lottery.common.weixin.pay.beans.OrderNotifyRes;
import com.xiaoluo.lottery.entity.HbInfo;
import com.xiaoluo.lottery.entity.Order;
import com.xiaoluo.lottery.filter.HbFilter;
import com.xiaoluo.lottery.service.HbInfoServiceImpl;
import com.xiaoluo.lottery.service.WeiXinServiceImpl;
import com.xiaoluo.lottery.vo.DataPage;
import com.xiaoluo.lottery.vo.HbInfoSumVo;
import com.xl.property.PropertyEtcdConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Project : lottery
 * Package : com.xiaoluo.lottery.web
 * Created by Willian.
 * Date  : 2016/4/6 10:14.
 *
 * 1、获取红包列表
 * 2、重新发放红包
 */
@Controller
@RequestMapping(value = "/hb")
public class HbInfoController {

    @Autowired
    private WeiXinServiceImpl payServiceImpl;
    @Autowired
    private HbInfoServiceImpl hbInfoService;

    @Autowired
    private PropertyEtcdConfigurer propertyConfigurer;

    private final static String SESSION_USER_KEY="SESSION_USER_KEY";


    @RequestMapping(value="/")
    public String index(ModelMap model,HttpServletRequest request){

        return "/hb/login";
    }
    @RequestMapping(value="/login")
    public String login(ModelMap model,
                        @RequestParam(value = "inputEmail", defaultValue = "") String inputEmail,
                        @RequestParam(value = "inputPassword", defaultValue = "") String inputPassword,
                        HttpServletRequest request){

        if(doAuth(inputEmail,inputPassword,request)){
            return "redirect:list";
        }

        return "/hb/login";
    }

    @RequestMapping(value="/doSendHb")
    public String doSendHb(ModelMap model,
                           @RequestParam(value = "hbcode", defaultValue = "") String hbcode,
                           HttpServletRequest request){

        if(isAuth(request)){
            RedPackRes res = payServiceImpl.reSendHb(hbcode);
            model.addAttribute("res",res);

            return "redirect:list";
        }else {
            return "redirect:login";
        }

    }

    @RequestMapping(value="/refresh")
    public String refresh(ModelMap model,
                           @RequestParam(value = "hbcode", defaultValue = "") String hbcode,
                           HttpServletRequest request){

        if(isAuth(request)){
            HbInfo res = payServiceImpl.gethbinfo(hbcode);
            model.addAttribute("res",res);

            return "redirect:list";
        }else {
            return "redirect:login";
        }

    }

    @RequestMapping(value="/list")
    public String hbList(ModelMap model,
                         HbFilter filter,
                         HttpServletRequest request){

        if(isAuth(request)){
            DataPage<HbInfo> page = hbInfoService.findAll(filter);
            HbInfoSumVo sumVo = hbInfoService.sum(filter);
            model.addAttribute("page",page);
            model.addAttribute("sumVo",sumVo);
            model.addAttribute("filter",filter);

            return "/hb/list";
        }else {
            return "redirect:login";
        }
    }

    /**
     *
     * @param request
     * @return
     */
    private boolean isAuth(HttpServletRequest request){
        return request.getSession().getAttribute(SESSION_USER_KEY) != null?true:false;
    }

    /**
     *
     * @param inputEmail
     * @param inputPassword
     * @param request
     * @return
     */
    private boolean doAuth(String inputEmail,String inputPassword,HttpServletRequest request){

        if("".equals(inputEmail) || "".equals(inputPassword)){
            return false;
        }
        String userName = propertyConfigurer.getProperty("admin.userName")==null ?"":propertyConfigurer.getProperty("admin.userName").toString();
        String passWord = propertyConfigurer.getProperty("admin.passWord")==null ?"":propertyConfigurer.getProperty("admin.passWord").toString();

        if(userName.equals(inputEmail) && passWord.equals(inputPassword)){
            request.getSession().setAttribute(SESSION_USER_KEY,"OK");
            return true;
        }else {
            request.getSession().removeAttribute(SESSION_USER_KEY);
        }
        return false;
    }


}
